#!/usr/bin/env python
# encoding: utf-8
from _socket import AF_INET, SOCK_STREAM
import json
from socket import socket
from settings import PLUGIN_MESSAGE_BODY, FLAG
from win_service_manage_helper import start_winservice

facedeteive_server_port = 8111

__author__ = '马志'

"""

by: 马志 at: 2016/9/5 14:46
@contact: hautmz@163.com
@file: win_service_helper.py
"""

start_winservice()

def win_service_socket_helper(data):
    start_winservice()
    host = "127.0.0.1"
    addr = (host, facedeteive_server_port)
    client = socket(AF_INET, SOCK_STREAM)
    client.settimeout(3)
    buffer_size = 1024
    try:
        client.connect(addr)
        client.send(data.encode('utf-8'))
        response_str = ""
        while True:
            receive_data = client.recv(buffer_size)
            if len(receive_data) > len(FLAG):
                response_str = response_str + receive_data
                if receive_data[-2:] == FLAG:
                    return json.loads(response_str[0:-2])
        client.close()
    except Exception, e:
        print e
        return None



def create_plugin_message(message_type, message_body, task_id=0, suorce_id=0):
    return json.dumps(
        {"PluginMessageType": message_type, "TaskId": task_id, "SourceId": suorce_id,
         "MessageBody": message_body}) + FLAG

def create_get_service_path():
    """

    by: 马志 at: ${DATE} ${TIME}
    """
    data = create_plugin_message(PLUGIN_MESSAGE_BODY["GetServicePath"], "")
    return win_service_socket_helper(data)


def get_log_count_from_service():
    data = create_plugin_message(PLUGIN_MESSAGE_BODY["GetLogTotalCount"], "")
    return win_service_socket_helper(data)

def get_logs_from_service(request):
    data = create_plugin_message(PLUGIN_MESSAGE_BODY["GetLogPageData"], json.dumps(request))
    return win_service_socket_helper(data)

def clear_blank():
    data = create_plugin_message(PLUGIN_MESSAGE_BODY["DelAllBlank"], "")
    return win_service_socket_helper(data)

def get_blank_page(request):
    data = create_plugin_message(PLUGIN_MESSAGE_BODY["GetBlankPage"], json.dumps(request))
    return win_service_socket_helper(data)


def get_blank_count_from_server():
    data = create_plugin_message(PLUGIN_MESSAGE_BODY["GetBlankCount"], None)
    return win_service_socket_helper(data)


def get_blank_item_from_server(guid):
    request = create_plugin_message(PLUGIN_MESSAGE_BODY["GetBlank"], json.dumps({"Guid":guid}))
    return win_service_socket_helper(request)

def del_blank_by_guid_from_server(guid):
    request = create_plugin_message(PLUGIN_MESSAGE_BODY["DelBlankByGuid"], json.dumps({"Guid":guid}))
    return win_service_socket_helper(request)


def add_blank_to_facedeteive_server(request_json):
    # host = "127.0.0.1"
    # addr = (host, facedeteive_server_port)
    # client = socket(AF_INET, SOCK_STREAM)
    # client.settimeout(10)
    # buffer_size = 1024
    data = create_plugin_message(PLUGIN_MESSAGE_BODY["AddBlank"], json.dumps(request_json))
    return win_service_socket_helper(data)
    # try:
    #     client.connect(addr)
    #     client.send(data.encode('utf-8'))
    #     response_str=""
    #     while True:
    #         receive_data = client.recv(buffer_size)
    #         if len(receive_data) > len(FLAG):
    #             response_str=response_str+receive_data
    #             if receive_data[-2:] == FLAG:
    #                 return json.loads(response_str[0:-2])
    # except Exception, e:
    #     print e
    #     return None
